package com.study.example.gameserver;

import com.sparrow.cache.EnableSparrowCache;
import com.sparrow.common.EnableSparrowCommon;
import com.sparrow.common.handler.HandlerManager;
import com.sparrow.common.thread.ThreadPool;
import com.sparrow.common.tick.GameLoop;
import com.sparrow.common.util.PropertiesUtil;
import com.sparrow.common.uuid.TimeUUIDGenerator;
import com.sparrow.event.EnableSparrowEvent;
import com.sparrow.resource.EnableSparrowResource;
import com.sparrow.rpc.EnableSparrowRpc;
import com.sparrow.rpc.manager.GameRpcNodeManager;
import com.sparrow.socket.http.EnableSparrowHttp;
import com.sparrow.socket.manager.PacketHandlerManager;
import com.sparrow.socket.tcp.EnableSparrowTcp;
import com.sparrow.socket.tcp.api.TcpServer;
import com.sparrow.socket.tcp.session.manager.GameSessionManager;
import com.study.example.gameserver.socket.handler.GameServerInitializer;
import com.study.example.gameserver.socket.handler.GameServerHandler;
import com.sparrow.socket.tcp.init.TcpServerInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableSparrowCommon
@EnableSparrowCache
@EnableSparrowResource
@EnableSparrowRpc
@EnableSparrowEvent
@EnableSparrowHttp
@EnableSparrowTcp
@EnableTransactionManagement
@ComponentScan({
        "com.study.example.gameserver"
})
public class GameConfig {

    @Bean
    public GameServerHandler gameServerHandler() {
        return new GameServerHandler();
    }

    @Bean
    public TcpServerInitializer tcpServer(GameServerHandler gameServerHandler) {
        GameServerInitializer serverInitializer = new GameServerInitializer();
        serverInitializer.setGameServerHandler(gameServerHandler);
        return serverInitializer;
    }

    @Bean
    public GameSessionManager gameSessionManager() {
        return new GameSessionManager();
    }

    @Bean
    public TcpServer nettyServer(TcpServerInitializer tcpServerInitializer) {
        TcpServer nettyServer = new TcpServer();
        nettyServer.setChannelInitializer(tcpServerInitializer);
        nettyServer.start(Integer.parseInt(PropertiesUtil.getProperty("server.port")));
        return nettyServer;
    }

    @Bean
    public TimeUUIDGenerator idGenerator() {
        String val = PropertiesUtil.getProperty("server.id");
        return new TimeUUIDGenerator(Integer.parseInt(val));
    }

    @Bean
    public GameRpcNodeManager nodeMgr() {
        return new GameRpcNodeManager();
    }

//    @Bean
//    public PerfTimerManager perfTimerRegistry() {
//        return new PerfTimerManager();
//    }

    @Bean
    public HandlerManager processorRegister() {
        return new HandlerManager();
    }

    @Bean
    public PacketHandlerManager packetHandlerManager() {
        return new PacketHandlerManager();
    }

    @Bean
    public GameLoop gameLoop() {
        GameLoop gameLoop = new GameLoop();
        ThreadPool.TICKER_EXECUTOR.execute(gameLoop);
        return gameLoop;
    }
}
